<script setup lang="ts">
defineOptions({
  name: "AllocateList",
  inheritAttrs: false,
});

import { getAllocatePage } from "@/api/customer/allocate";
import { AllocateQuery, AllocateItem } from "@/api/customer/allocate/types";

const queryFormRef = ref(ElForm);

const loading = ref(false);
const total = ref(0);
const customerAddRef = ref();
const customerDetailRef = ref();

const queryParams = reactive<AllocateQuery>({
  pageNum: 1,
  pageSize: 15,
});

const pageList = ref<AllocateItem[]>();

const resetQuery = () => {
  queryParams.keyword = "";
  handleQuery();
};

const handleQuery = async () => {
  loading.value = true;
  const { data } = await getAllocatePage(queryParams);
  pageList.value = data.list;
  total.value = data.total;
  loading.value = false;
};

const openDialog = () => {
  customerAddRef.value.showDialog();
};

const handleSelectionChange = (rows: AllocateItem[]) => {};

// 客资详情
const handleDetail = (row: AllocateItem) => {
  customerDetailRef.value.showDialog(row.id);
};

// 转发
const handleShare = (row: AllocateItem) => {
  console.log(row);
};

onMounted(() => {
  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <div class="search-container" v-hasPerm="['customer:search']">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keyword" label="关键字">
          <el-input
            v-model="queryParams.keyword"
            placeholder="角色名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="pageList"
        highlight-current-row
        border
        fit
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="跟进人" align="center">
          <template #default="scope">
            <el-link type="primary" @click="handleDetail(scope.row)">{{
              scope.row.user.realname
            }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="客户姓名" prop="member.fullname" />
        <el-table-column label="联系方式" prop="member.contact.contact">
          <template #default="scope">
            <span>{{ scope.row.member.contact.contact }}</span>
            <el-link type="primary" @click="handleDetail(scope.row)"
              >查看</el-link
            >
          </template>
        </el-table-column>
        <el-table-column label="跟进状态" prop="visit_status.title" />
        <el-table-column label="渠道名称" prop="member.source.title" />
        <el-table-column label="下发日期" prop="create_time" />
        <el-table-column label="备注" prop="member.remark" />
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <customer-add
      ref="customerAddRef"
      @handleQuery="handleQuery"
    ></customer-add>
    <customer-detail ref="customerDetailRef"></customer-detail>
  </div>
</template>
